class User {
    String name
    City city
    String email
    String gender
    String friendGender
    static hasMany = [ownedBooks:BookInstance, wantedBooks:BookProfile]

    static constraints = {
        name(maxLength:30,blank:false,unique:true)
        
        city(blank:false)
        gender(blank:false, inList:['M','F'])
        friendGender(blank:false, inList:['M','F','N/A'])

        email(blank:false,
            validator: {val, obj ->
                if(!val.contains('@')) return false
                if ( !obj.id ) {
                    return User.findByEmailIlike(val) == null
                } else {
                    def dbObj = User.findByEmailIlike(val)
                    if (dbObj) {
                        return dbObj.id == obj.id
                    }
                }
            })
    }

    String toString() {name}
}
